package com.yanqu.road.dao.impl.activity.douluodalu;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.douluodalu.config.DouLuoDaLuEventInfo;
import com.yanqu.road.entity.activity.douluodalu.config.DouLuoDaLuRegionInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class DouLuoDaLuConfigDaoImpl extends TempDao {

    private static String getDouLuoDaLuEventInfoSql = "select * from `t_s_activity_douluofight_event` where `activityId`=? ORDER BY `id` ASC";
    public List<DouLuoDaLuEventInfo> getDouLuoDaLuEventInfoList(int activityId) {
        List<DouLuoDaLuEventInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getDouLuoDaLuEventInfoSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                DouLuoDaLuEventInfo douLuoDaLuEventInfo = new DouLuoDaLuEventInfo();
                douLuoDaLuEventInfo.setActivityId(rs.getInt("activityId"));
                douLuoDaLuEventInfo.setEventId(rs.getInt("id"));
                douLuoDaLuEventInfo.setEventType(rs.getInt("eventType"));
                douLuoDaLuEventInfo.setTargetId(rs.getString("targetId"));
                douLuoDaLuEventInfo.setScore(rs.getInt("score"));
                douLuoDaLuEventInfo.setRewards(rs.getString("reward"));
                douLuoDaLuEventInfo.setParam(rs.getString("param"));
                douLuoDaLuEventInfo.setShowLog(rs.getBoolean("isShowLog"));
                list.add(douLuoDaLuEventInfo);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    private static String getDouLuoDaLuRegionInfoSql = "select * from `t_s_activity_douluofight_region` where `activityId`=? ORDER BY `id` ASC";
    public List<DouLuoDaLuRegionInfo> getDouLuoDaLuRegionInfoList(int activityId) {
        List<DouLuoDaLuRegionInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getDouLuoDaLuRegionInfoSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                DouLuoDaLuRegionInfo douLuoDaLuRegionInfo = new DouLuoDaLuRegionInfo();
                douLuoDaLuRegionInfo.setActivityId(rs.getInt("activityId"));
                douLuoDaLuRegionInfo.setRegionId(rs.getInt("id"));
                douLuoDaLuRegionInfo.setEnergy(rs.getInt("energy"));
                douLuoDaLuRegionInfo.setUpgradeConsumeList(StringUtils.stringToStringList(rs.getString("consumeCondition"), "\\|"));
                douLuoDaLuRegionInfo.setRingId(rs.getInt("ringId"));
                douLuoDaLuRegionInfo.setScoreBase(rs.getInt("scoreBase"));
                String eventWeightStr = rs.getString("eventWeight");
                List<Integer> eventIdList = new ArrayList<>();
                List<Integer> eventWeightList = new ArrayList<>();
                if(!StringUtils.isNullOrEmpty(eventWeightStr)){
                    List<String> eventWeightStrList = StringUtils.stringToStringList(eventWeightStr, "\\|");
                    for (String eventWeight : eventWeightStrList) {
                        List<Integer> valList = StringUtils.stringToIntegerList(eventWeight, ";");
                        eventIdList.add(valList.get(0));
                        eventWeightList.add(valList.get(1));
                    }
                }
                douLuoDaLuRegionInfo.setEventIdList(eventIdList);
                douLuoDaLuRegionInfo.setEventWeightList(eventWeightList);
                list.add(douLuoDaLuRegionInfo);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

}
